package com.hs.srm.admin.modules.abnormalquality.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.abnormalquality.dao.AbnormalQualityDao;
import com.hs.srm.admin.modules.abnormalquality.entity.AbnormalQualityEntity;
import com.hs.srm.admin.modules.abnormalquality.service.AbnormalQualityService;
import com.hs.srm.admin.modules.notice.dictionary.NoticeKind;
import com.hs.srm.admin.modules.notice.service.NoticeMainInfoService;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 品质异常单服务实现类
 *
 * @author ych
 * @since 2020-04-07 15:02:27
 */
@Service("abnormalQualityService")
public class AbnormalQualityServiceImpl extends ServiceImpl<AbnormalQualityDao, AbnormalQualityEntity> implements AbnormalQualityService {

    @Autowired
    private AbnormalQualityDao abnormalQualityDao;

    @Autowired
    private NoticeMainInfoService noticeMainInfoService;


    public PageUtils queryPage(Map<String, Object> params) {
        // 负责单位
        String vendorCode = (String) params.get("vendorCode");
        //供应商名称
        String vendorName = (String) params.get("vendorName");
        //审核结果，审核结果:0，审核中；1，审核通过；-1，审核驳回
        String auditResult = (String) params.get("auditResult");
        //物料编码
        String materialCode = (String) params.get("materialCode");
        //物料描述
        String materialDesc = (String) params.get("materialDesc");
        //发文时间起
        String startDate = (String) params.get("startDate");
        //发文时间止
        String endDate = (String) params.get("endDate");
        //供应商编码
        String sapCode = (String) params.get("sapCode");
        Page<AbnormalQualityEntity> page = this.selectPage(
                new QueryParam<AbnormalQualityEntity>(params).getPage(),
                new EntityWrapper<AbnormalQualityEntity>()
                        .eq(StringUtils.isNotBlank(vendorCode), "vendor_code", vendorCode)
                        .eq(StringUtils.isNotBlank(sapCode), "vendor_code", sapCode)
                        .like(StringUtils.isNotBlank(vendorName), "vendor_name", vendorName)
                        .eq(StringUtils.isNotBlank(auditResult), "audit_result", auditResult)
                        .like(StringUtils.isNotBlank(materialDesc), "material_desc", materialDesc)
                        .ge(StringUtils.isNotBlank(startDate), "submit_date", startDate)
                        .le(StringUtils.isNotBlank(endDate), "submit_date", endDate)
                        .eq(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .orderBy("create_date", false)
        );
        return new PageUtils(page);
    }

    /**
     * 供应商填写品质异常单，通知品质人员
     *
     * @param kind    通知类型
     * @param content 通知内容
     */
    @Override
    public void sendNotice(NoticeKind.KindEnum kind, String content) {
        noticeMainInfoService.saveNotice(kind, content);
    }
}